const CryptoJS = require("crypto-js");
require('dotenv').config();

exports.encrypt = function (msg) {
    const secretKey = CryptoJS.enc.Hex.parse(process.env.AES_SECRET_KEY);
    const dataHex = CryptoJS.enc.Utf8.parse(msg);
    const encrypted = CryptoJS.AES.encrypt(dataHex, secretKey, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
}

exports.decrypt = function (msg) {
    const secretKey = CryptoJS.enc.Hex.parse(process.env.AES_SECRET_KEY);
    const encryptedHexStr = CryptoJS.enc.Hex.parse(msg);
    const str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    const decrypt = CryptoJS.AES.decrypt(str, secretKey, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return decrypt.toString(CryptoJS.enc.Utf8);
}